package com.foreknow.elm.dao.impl;


import com.foreknow.elm.dao.DeliveryAddressDao;
import com.foreknow.elm.po.Deliveryaddress;
import com.foreknow.elm.util.DBUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DeliveryAddressDaoImpl  implements DeliveryAddressDao {
    private Connection con = null;
    private PreparedStatement pst = null;
    private ResultSet rs = null;
    @Override
    public List<Deliveryaddress> listDeliveryAddressByUserId(String userId) throws Exception {
        List<Deliveryaddress> list = new ArrayList<>();
        String sql = "select * from deliveryaddress where userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while (rs.next()){
                Deliveryaddress deliveryaddress = new Deliveryaddress();
                deliveryaddress.setDaId(rs.getInt("daId"));
                deliveryaddress.setContactName(rs.getString("contactName"));
                deliveryaddress.setContactSex(rs.getInt("contactSex"));
                deliveryaddress.setContactTel(rs.getString("contactTel"));
                deliveryaddress.setAddress(rs.getString("address"));
                list.add(deliveryaddress);
            }
        }finally{
            DBUtil.close(rs,pst);
        }

        return list;
    }

    @Override
    public Deliveryaddress getDeliveryAddressById(Integer daId) throws Exception {
        Deliveryaddress deliveryaddress= new Deliveryaddress();
        String sql = "select * from deliveryaddress where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, daId);
            rs = pst.executeQuery();
            while (rs.next()){
                deliveryaddress.setDaId(rs.getInt("daId"));
                deliveryaddress.setContactName(rs.getString("contactName"));
                deliveryaddress.setContactSex(rs.getInt("contactSex"));
                deliveryaddress.setContactTel(rs.getString("contactTel"));
                deliveryaddress.setAddress(rs.getString("address"));
                deliveryaddress.setUserId(rs.getString("userId"));
            }
        }finally{
            DBUtil.close(rs,pst);
        }
        return deliveryaddress;
    }

    @Override
    public int saveDeliveryAddress(Deliveryaddress deliveryaddress) throws Exception {
        int result =0;
        String sql = "insert into deliveryaddress(daId,contactName,contactSex,contactTel,address,userId) values(?,?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,deliveryaddress.getDaId());
            pst.setString(2,deliveryaddress.getContactName());
            pst.setInt(3,deliveryaddress.getContactSex());
            pst.setString(4,deliveryaddress.getContactTel());
            pst.setString(5,deliveryaddress.getAddress());
            pst.setString(6,deliveryaddress.getUserId());
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int updateDeliveryAddress(Deliveryaddress deliveryaddress) throws Exception {
        int result =0;
        String sql = "update deliveryaddress set contactName=?,contactSex=?,contactTel=?,address=?,userId=? where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,deliveryaddress.getContactName());
            pst.setInt(2,deliveryaddress.getContactSex());
            pst.setString(3,deliveryaddress.getContactTel());
            pst.setString(4,deliveryaddress.getAddress());
            pst.setString(5,deliveryaddress.getUserId());
            pst.setInt(6,deliveryaddress.getDaId());

            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);

        }
        return result;
    }

    @Override
    public int removeDeliveryAddress(Integer daId) throws Exception {
        int result =0;
        String sql = "delete from deliveryaddress where daId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,daId);
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);

        }
        return result;
    }
}
